package com.yss.auto;

import com.yss.adapter.IAdapter;
import com.yss.config.ParallelConfig;
import com.yss.config.RouterConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;

@Slf4j
@Configuration
@ComponentScan("com.yss")
@EnableConfigurationProperties
@Profile(value = "router-inner")
public class RouterParallelAutoConfiguration {

    @Autowired
    private RouterConfig routerConfig;
    @Autowired
    private ParallelConfig parallelConfig;

    static {
        log.info("\n{}", "RouterParallelAutoConfiguration has been started.");
    }

    @PostConstruct
    public void init() {
        Map<String, List<IAdapter>> routerRuleMapper = routerConfig.getRuleAdaptersMapper();
        Map<String, List<IAdapter>> parallelRuleMapper = parallelConfig.getRuleAdaptersMapper();

        log(routerRuleMapper);
        log(parallelRuleMapper);
    }

    private void log(Map<String, List<IAdapter>> mapper) {
        mapper.forEach((k, v) -> {
            StringBuilder strBuilder = new StringBuilder();
            if (v != null && CollectionUtils.isNotEmpty(v)) {
                v.forEach(iAdapter -> strBuilder.append("\n  -" + iAdapter.getClass().getName()));
            }
            log.info("\n{}{}", k, strBuilder.toString());
        });
    }
}
